package com.prompt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import com.prompt.AlarmActivity.EndListener;
import com.prompt.SetAdapter.ViewHolder;
import com.prompt.utils.TimeUtil;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainSetActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_set_list);

		Button btAgain =(Button)findViewById(R.id.bt_again_today); 
		btAgain.setOnClickListener(new TodayAgainListener());   
		 
		// 绑定XML中的ListView，作为Item的容器
		ListView list = (ListView) findViewById(R.id.mainSetList);

		// 生成动态数组，并且转载数据
		ArrayList<SetVO> mylist = new ArrayList<SetVO>();
		for (int i = 0; i < 3; i++) {
			SetVO vo = new SetVO();
			vo.setTitle("This is a Title"+i);
			mylist.add(vo);
		}
		setListData(mylist);
		SetAdapter mSchedule = new SetAdapter(this, mylist);
				
		// 添加并且显示
		list.setAdapter(mSchedule);
		list.setOnItemClickListener(new OnItemClickListener() {
			 
            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long arg3) {
                ViewHolder holder = (ViewHolder) view.getTag();
                holder.selected.toggle();// 在每次获取点击的item时改变checkbox的状态
//                MyAdapter.isSelected.put(position, holder.cb.isChecked()); // 同时修改map的值保存状态
                String selectStr = "";
                int state = 0;
                if (holder.selected.isChecked() == true) {
                	selectStr = "已选中"+holder.title.getText();
                	state = 1;
                } else {
                	selectStr = "已取消"+holder.title.getText();
                	state = 0;
                }
                SharedPreferences listSet = getSharedPreferences("listSet", 0); 
                Editor setEdit = null;
                setEdit = listSet.edit();
            	setEdit.putString("item"+position,String.valueOf(state));  
            	setEdit.commit();
                Toast.makeText(getApplicationContext(), selectStr, Toast.LENGTH_SHORT).show();
                
            }
        });
		

	}//onCreate

	 class TodayAgainListener implements OnClickListener{
		public void onClick(View v) {   
			setTodayAgain();
		}
	 }
	 
	private void setTodayAgain(){
		
		Timer mTimer = new Timer();                
		mTimer.schedule(new TimerTask() {   
            @Override 
            public void run() { 
            	setTodayAgainLazy();                                
            }
		}, 10*1000);

		Toast.makeText(getApplicationContext(), "设置成功，10秒后生效", Toast.LENGTH_SHORT).show();
	}
		
	private void setTodayAgainLazy(){
		String today = TimeUtil.getTodayStr();
		GlobalState.getInstance().addAgain(today);
		
		SharedPreferences dateState = getSharedPreferences("dateState", 0); 
        Editor setEdit = null;
        setEdit = dateState.edit();
    	setEdit.putString(today,"0");  
    	setEdit.commit();
	}
	
	private void setListData(ArrayList<SetVO> mylist){
		if(mylist == null)
			return;
        SharedPreferences listSet = getSharedPreferences("listSet", 0); 
        String itemState;
        for(int i=0;i<mylist.size();i++){
        	itemState = listSet.getString("item"+i, null);  
        	if(itemState == null || itemState.equals("0")){
        		mylist.get(i).setSelect(false);
        	}else{
        		mylist.get(i).setSelect(true);
        	}
        }
	}
	
	private void initSetting(){
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;

	}
}
